package link.tengfei.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import link.tengfei.bean.AdminUser;
import link.tengfei.bean.GengralUser;
import link.tengfei.util.ConnectionFactory;

public class UserRegisterDaoImpl implements UserRegisterDao {

	@Override
	public boolean findByUsername(String username) {
		boolean flag = false;
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		String sql = "select id from admin_user where username =? union select id from gengral_user where username =?";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, username);
			psmt.setString(2, username);
			rs = psmt.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				if (id > 0) {
					flag = true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return flag;
	}

	@Override
	public int addGengralUser(GengralUser gengralUser) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int result = 0;
		String sql = "insert into gengral_user (name,sex,age,number,username,password,email,total,add_date,delete_date,state,change_user) values (?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, gengralUser.getName());
			psmt.setInt(2, gengralUser.getSex());
			psmt.setString(3, gengralUser.getAge());
			psmt.setString(4, gengralUser.getNumber());
			psmt.setString(5, gengralUser.getUsername());
			psmt.setString(6, gengralUser.getPassword());
			psmt.setString(7, gengralUser.getEmail());
			psmt.setInt(8, gengralUser.getTotal());
			psmt.setString(9, gengralUser.getAdddate());
			psmt.setString(10, gengralUser.getDeletedate());
			psmt.setInt(11, gengralUser.getState());
			psmt.setString(12, gengralUser.getChangeuser());
			result = psmt.executeUpdate();
			return result;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return result;
	}

	@Override
	public boolean findByUserId(String userId) {
		boolean flag = false;
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		String sql = "select id from gengral_user where number =?";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, userId);
			rs = psmt.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				if (id > 0) {
					flag = true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return flag;
	}

	@Override
	public boolean findByPassId(int passid) {
		boolean flag = true;
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		ResultSet rs = null;
		String sql = "select id from admin_user where username is null and name is null and pass_id=?";
		try {
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, passid);
			rs = psmt.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("id");
				if (id > 0) {
					flag = false;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, rs);
		}
		return flag;
	}

	@Override
	public int addAdminUser(AdminUser adminUser) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int result = 0;
		try {
			String sql = "update admin_user set name=?,sex=?,age=?,username=?,password=?,email=?,binding_date=?,judge=?,state=? where pass_id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, adminUser.getName());
			psmt.setInt(2, adminUser.getSex());
			psmt.setString(3, adminUser.getAge());
			psmt.setString(4, adminUser.getUsername());
			psmt.setString(5, adminUser.getPassword());
			psmt.setString(6, adminUser.getEmail());
			psmt.setString(7, adminUser.getBindingdate());
			psmt.setInt(8, adminUser.getJudge());
			psmt.setInt(9, adminUser.getState());
			psmt.setInt(10, adminUser.getPassid());
			result = psmt.executeUpdate();
			return result;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return result;
	}

}
